//
//  SMACTabbarController.swift
//  SmiraAiChat
//
//  Created by mumu on 2024/9/20.
//

import UIKit

class SMACTabbarController: UITabBarController {

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        tabBar.frame = CGRect(origin: CGPoint.init(x: 16.x_bili, y:  UIScreen.main.bounds.height - 56.x_bili - SMACCommen.bottomSafehit_SMAC - 8.x_bili), size: CGSize.init(width: SMACCommen.scrwid_SMAC - 32.x_bili, height: 56.x_bili))
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.tabBar.isTranslucent = true
        self.tabBar.backgroundColor = UIColor.white

        let appearance = UITabBarAppearance.init()
        appearance.backgroundColor = UIColor.white

        self.tabBar.standardAppearance = appearance
        tabBar.layer.borderColor = UIColor.black.cgColor
        tabBar.layer.borderWidth = 1

        self.tabBar.tintColor = UIColor.white
        
        tabBar.frame = CGRect(origin: CGPoint.init(x: 16.x_bili, y:  UIScreen.main.bounds.height - 56.x_bili - SMACCommen.bottomSafehit_SMAC - 8.x_bili), size: CGSize.init(width: SMACCommen.scrwid_SMAC - 32.x_bili, height: 56.x_bili))
        tabBar.layer.cornerRadius = 16

        self.viewControllers = [createChildController_SMAC(iconnam: "icon_home", rootvcAcma: SMACHomeController.init()),
                         createChildController_SMAC(iconnam: "icon_square", rootvcAcma: SMACPublishController.init()),
                         createChildController_SMAC(iconnam: "icon_publish", rootvcAcma: SMACSquareController.init()),
                         createChildController_SMAC(iconnam: "icon_mine", rootvcAcma: SMACMineController.init())]
        
        

        let bgLayer1 = CALayer()
        bgLayer1.frame = CGRect(x: 1, y: 1, width: SMACCommen.scrwid_SMAC - 32.x_bili - 1, height: 56.x_bili)
        bgLayer1.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 1).cgColor
        bgLayer1.cornerRadius = 16
        tabBar.layer.insertSublayer(bgLayer1, at: 0)//.addSublayer(bgLayer1)
        // shadowCode
        tabBar.layer.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 1).cgColor
        tabBar.layer.shadowOffset = CGSize(width: 0, height: 2)
        tabBar.layer.shadowOpacity = 1
        tabBar.layer.shadowRadius = 0

    }
    
    func createChildController_SMAC(iconnam:String,rootvcAcma:UIViewController) -> SMACTopNviController {
        let navi = SMACTopNviController.init(rootViewController: rootvcAcma)
        
        rootvcAcma.tabBarItem.image = UIImage.init(named: iconnam)?.withRenderingMode(.alwaysOriginal)
        rootvcAcma.tabBarItem.selectedImage = UIImage.init(named:"sel_" + iconnam)?.withRenderingMode(.alwaysOriginal)
        return navi
    }
}


class SMACTopNviController: UINavigationController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationBar.isHidden = true
        
        let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
        UIGraphicsBeginImageContextWithOptions(rect.size, false, 0)
        let bgtn = UIGraphicsGetCurrentContext()
        bgtn?.setFillColor(UIColor.clear.cgColor)
        bgtn?.fill(rect)
      
        let clearImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        
        self.navigationBar.setBackgroundImage(clearImage, for: .default)
        self.navigationBar.shadowImage = clearImage
        self.navigationBar.isTranslucent = true
        UINavigationBar.appearance().tintColor = .clear
    }
    
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        if children.count > 0  && !viewController.isMember(of:SMACBasicController.self) {
            viewController.hidesBottomBarWhenPushed = true
        }
        super.pushViewController(viewController, animated: false)
    }
    

}


class SMACBasicController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .black
    }
}
